package com.daxia.ogmall.web.controller.admin;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.daxia.ogmall.common.Logs;
import com.daxia.ogmall.common.ProductCategoryStatus;
import com.daxia.ogmall.dto.JsonResultDTO;
import com.daxia.ogmall.dto.ProductCategoryDTO;
import com.daxia.ogmall.model.ProductCategory;
import com.daxia.ogmall.service.ProductCategoryService;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.web.controller.BaseController;

@Controller
@RequestMapping(value = "/admin/productCategory", produces="text/html;charset=UTF-8")
public class AdminProductCategoryController extends BaseController {
	private static Logger logger = Logger.getLogger(AdminProductCategoryController.class);
	
	@Autowired
	private ProductCategoryService productCategoryService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id) {
		if (id != null) {
			ProductCategoryDTO dto = productCategoryService.load(id);
			map.put("n", dto);
		}
		map.put("pcss", ProductCategoryStatus.values());
		ProductCategoryDTO query = new ProductCategoryDTO();
		query.setLevel(1);
		List<ProductCategoryDTO> topCategories = productCategoryService.find(query, null);
		map.put("topCategories", topCategories);
		return "admin/productCategory/productCategory_detail";
	}
	
	@ResponseBody
	@RequestMapping(value = "/save")
	
	public String save(ProductCategoryDTO dto, JsonResultDTO resultDTO) throws Exception {
		if (dto.getParentCategory() == null || dto.getParentCategory().getId() == null) {
			dto.setParentCategory(null);
		}
		if (dto.getId() == null) {
			productCategoryService.create(dto);
		} else {
			productCategoryService.updateAllFields(dto);
		}
		return okAndClose(resultDTO);
	}

    
	@ResponseBody
	@RequestMapping(value = "/delete") 
	public String delete(Long[] ids, JsonResultDTO resultDTO) throws IOException {
		productCategoryService.deleteByIds(ids);
		return okAndRefresh(resultDTO);
	}
	
	@RequestMapping(value = "/list")
	public String list(ProductCategoryDTO dto, Map<String, Object> map, Page page) {
		dto.setFindAll(true);
		List<ProductCategoryDTO> dtos = productCategoryService.find(dto, null);
		map.put("list", dtos);
		map.put("query", dto);
		map.put("pcss", ProductCategoryStatus.values());
		return "admin/productCategory/productCategory_list";
	}
	
	@RequestMapping(value = "/root")
	public String root(ProductCategoryDTO dto, Map<String, Object> map, Page page) {
		List<ProductCategoryDTO> dtos = productCategoryService.find(dto, null);
		map.put("list", dtos);
		map.put("query", dto);
		map.put("pcss", ProductCategoryStatus.values());
		return "admin/productCategory/productCategory_root";
	}
	
	@ResponseBody
	@RequestMapping(value = "/datagrid")
    
    public String datagrid(ProductCategoryDTO dto, Map<String, Object> map, Page page) throws Exception {
        List<ProductCategoryDTO> dtos = productCategoryService.find(dto, page);
        JSONArray jsonArray = new JSONArray();
        for (ProductCategoryDTO productCategory : dtos) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", productCategory.getId());
            jsonObject.put("name", productCategory.getName());
            jsonObject.put("status", productCategory.getStatus());
            jsonObject.put("code", productCategory.getCode());
            jsonObject.put("image", productCategory.getImage());
            jsonArray.add(jsonObject);
        }
        JSONObject json = new JSONObject();
        json.put("total", page.getTotalRecords());
        json.put("rows", jsonArray);
        return json.toJSONString();
    }
	
	@ResponseBody
    @RequestMapping("findCategory")
    public String findCategory(ProductCategory parent) throws Exception {
        if (parent.getId() != null) {
        	ProductCategoryDTO dto = new ProductCategoryDTO();
        	dto.setParentCategory(parent);
            List<ProductCategoryDTO> list = productCategoryService.find(dto, null);
            JSONArray array = new JSONArray();
            for (ProductCategoryDTO d : list) {
                JSONObject json = new JSONObject();
                json.put("id", d.getId());
                json.put("name", d.getName());
                array.add(json);
            }
            return array.toJSONString();
        }
        return null;
    }
	
	@ResponseBody
	@RequestMapping("upload")
	public String upload(HttpServletRequest request) throws Exception {
		MultipartHttpServletRequest mhs = (MultipartHttpServletRequest) request;  
        MultipartFile mf = mhs.getFile("file");
        Logs.COMMON.debug("mf: " + mf);
		productCategoryService.doUpload(mf);
		return okAndClose(new JsonResultDTO());
	}
}
